package be.xios.projectquiz.global;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;

/// <summary>
/// Deze klasse zorgt ervoor dat objecten tussen server/clients kunnen verzonden en ontvangen worden
/// </summary>
/// <author>Flemming Bosch: bundeling van de methods in 1 klasse</author>
/// <author>Sebastian Kubski: methods in de server en clients initieel geschreven</author>
public class StreamMethods {

	private Socket socket;
	private ObjectInputStream objectInput;
	private ObjectOutputStream objectOutput;
	private PrintWriter stringOutput;
	private BufferedReader stringInput;
	
	//Constructor (parameter: een socket)
	public StreamMethods(Socket socket){
		this.socket=socket;
	}
	
	public void openObjectOutputStream() {
		try {
			objectOutput = new ObjectOutputStream( socket.getOutputStream() );
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void openObjectInputStream() {
		try {
			//objectOutput.flush();
			objectInput  = new ObjectInputStream(  socket.getInputStream()  );
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void closeObjectInputStream( ) {
		try {
			objectInput.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void closeObjectOutputStream() {
		try {
			objectOutput.close();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unused")
	private void openStringOutputStream() {
		try {
			stringOutput = new PrintWriter(socket.getOutputStream() );
		}
		catch ( IOException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unused")
	private void closeStringOutputStream() {
		if ( stringOutput != null )  { 
			stringOutput.close();
		}
	}
	
	@SuppressWarnings("unused")
	private void openStringInputStream() {
		try {
			stringInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unused")
	private void closeStringInputStream() {
		if ( stringInput != null) {
			try {
				stringInput.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public ObjectInputStream getObjectInput() {
		return objectInput;
	}

	public void setObjectInput(ObjectInputStream objectInput) {
		this.objectInput = objectInput;
	}

	public ObjectOutputStream getObjectOutput() {
		return objectOutput;
	}

	public void setObjectOutput(ObjectOutputStream objectOutput) {
		this.objectOutput = objectOutput;
	}

	public PrintWriter getStringOutput() {
		return stringOutput;
	}

	public void setStringOutput(PrintWriter stringOutput) {
		this.stringOutput = stringOutput;
	}

	public BufferedReader getStringInput() {
		return stringInput;
	}

	public void setStringInput(BufferedReader stringInput) {
		this.stringInput = stringInput;
	}

}